using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using stsdev.Builders.SourceFiles;

namespace stsdev
{
    public class WebPartDescriptionBuilder
    {

        public static void Create(string WebPartName)
        {
            Create(WebPartName, string.Empty);
        }

        public static void Create(string WebPartName, string CatalogIconImageURL)
        {

            // create \TEMPLATE Folder
            if (!Directory.Exists(Globals.TemplatesFolder))
            {
                Directory.CreateDirectory(Globals.TemplatesFolder);
            }

            // create \TEMPLATE\FEATURE Folder
            if (!Directory.Exists(Globals.FeaturesFolder))
            {
                Directory.CreateDirectory(Globals.FeaturesFolder);
            }

            // create project-specific Feature Folder
            string SolutionSpecificFeatureFolder = Globals.FeaturesFolder + @"\" + SolutionBuilder.SolutionName;
            if (!Directory.Exists(SolutionSpecificFeatureFolder))
            {
                Directory.CreateDirectory(SolutionSpecificFeatureFolder);
            }

            // create project-specific Feature Folder
            string SolutionSpecificFeatureWebPartsFolder = SolutionSpecificFeatureFolder + @"\WebParts";
            if (!Directory.Exists(SolutionSpecificFeatureWebPartsFolder))
            {
                Directory.CreateDirectory(SolutionSpecificFeatureWebPartsFolder);
            }

            string WebPartDescriptionPath = SolutionSpecificFeatureWebPartsFolder + @"\" + WebPartName + ".webpart";
            StreamWriter manifestStream = new StreamWriter(WebPartDescriptionPath);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = true;
            settings.NewLineOnAttributes = false;
            settings.NewLineHandling = NewLineHandling.None;
            XmlWriter writer = XmlWriter.Create(manifestStream, settings);
            writer.WriteStartDocument();

            writer.WriteComment("Created by STSDEV at " + DateTime.Now.ToString());
            writer.WriteStartElement("webParts");
            string nsWP = "http://schemas.microsoft.com/WebPart/v3";
            writer.WriteStartElement("webPart", nsWP);
            writer.WriteStartElement("metaData");
            writer.WriteStartElement("type");
            string ClassName = SolutionBuilder.SolutionName + "." + WebPartName;
            string AssemblyName = SolutionBuilder.FullAssemblyName;
            writer.WriteAttributeString("name", ClassName + ", " + AssemblyName);
            writer.WriteEndElement(); // </type>
            writer.WriteElementString("importErrorMessage", "Error importing Web Part");
            writer.WriteEndElement(); // </metaData>
            writer.WriteStartElement("data");
            writer.WriteStartElement("properties");

            // property: Title
            writer.WriteStartElement("property");
            writer.WriteAttributeString("name", "Title");
            writer.WriteAttributeString("type", "string");
            writer.WriteValue("Default title for " + WebPartName);
            writer.WriteEndElement(); // </property>

            // property: Description
            writer.WriteStartElement("property");
            writer.WriteAttributeString("name", "Description");
            writer.WriteAttributeString("type", "string");
            writer.WriteValue("A demo Web Part created by stsdev");
            writer.WriteEndElement(); // </property>

            // property: ChromeState
            writer.WriteStartElement("property");
            writer.WriteAttributeString("name", "ChromeState");
            writer.WriteAttributeString("type", "chromestate");
            writer.WriteValue("Normal");
            writer.WriteEndElement(); // </property>

            // property: AllowZoneChange
            writer.WriteStartElement("property");
            writer.WriteAttributeString("name", "AllowZoneChange");
            writer.WriteAttributeString("type", "bool");
            writer.WriteValue("True");
            writer.WriteEndElement(); // </property>

            // property: AllowHide
            writer.WriteStartElement("property");
            writer.WriteAttributeString("name", "AllowHide");
            writer.WriteAttributeString("type", "bool");
            writer.WriteValue("True");
            writer.WriteEndElement(); // </property>

            // property: ExportMode
            writer.WriteStartElement("property");
            writer.WriteAttributeString("name", "ExportMode");
            writer.WriteAttributeString("type", "exportmode");
            writer.WriteValue("All");
            writer.WriteEndElement(); // </property>

            //property: CatalogIconImageUrl
            if (!string.IsNullOrEmpty(CatalogIconImageURL))
            {
                writer.WriteStartElement("property");
                writer.WriteAttributeString("name", "CatalogIconImageUrl");
                writer.WriteAttributeString("type", "string");
                writer.WriteValue(CatalogIconImageURL);
                writer.WriteEndElement(); //</property>
            }

            writer.WriteEndElement(); // </properties>
            writer.WriteEndElement(); // </data>
            writer.WriteEndElement(); // </webParts>

            writer.WriteEndElement(); // </webParts>
            writer.WriteEndDocument();
            writer.Close();
            manifestStream.Close();
            SolutionBuilder.SolutionProvider.ProjectProvider.AddSourceFile(new SourceFile(WebPartDescriptionPath, SourceFileType.None));
            FeatureBuilder.AddElementFile(@"WebParts\" + WebPartName + ".webpart");
        }
    }
}
